import aiohttp
import asyncio
import time
from config import ZHUQUE_COOKIE, ZHUQUE_CSRF,ZHUQUE_XC
import logging

logger = logging.getLogger("main")
# 配置区，运行前填好配置
cookie_headers = {
    "Cookie": ZHUQUE_COOKIE,
    "X-Csrf-Token": ZHUQUE_CSRF,
}
# 表示抽奖多少次


# 奖品和价值的字典
prizes = {
    1: "改名卡",
    2: "神佑7天卡",
    3: "邀请卡",
    4: "自动释放7天卡",
    5: "20G",
    6: "10G",
    7: "谢谢惠顾",
}
values = {1: 300000, 2: 100000, 3: 80000, 4: 30000}


url = "https://zhuque.in/api/gaming/spinThePrizeWheel"
lock = asyncio.Lock()

total_spins = 0
# lock = RLock()
prize_counts = {k: 0 for k in prizes.keys()}
upload_in_gb = 0
cost = 0
bonus_back = 0


async def tspin_the_wheel(number):

    async with aiohttp.ClientSession() as session:
        tasks = []
        share = ZHUQUE_XC
        base_value = number // share
        remainder = number % share
        numbers = [base_value] * share
        for i in range(remainder):
            numbers[i] += 1
        for i in range(share):
            tasks.append(fetch_prize(numbers[i], session))

        # 并发执行所有协程
        await asyncio.gather(*tasks)


async def fetch_prize(number, session):
    global cost
    global bonus_back
    global upload_in_gb
    global prize_counts
    for i in range(number):
        async with session.post(url, headers=cookie_headers) as response:
            if response.status == 200:
                json_response = await response.json()
                if json_response:
                    prize_index = json_response.get("data", {}).get("prize")
                    if prize_index is not None:
                        prize_index = int(prize_index)
                        async with lock:
                            prize_counts[prize_index] += 1
                            cost += 1500  # 假设每次抽奖消耗1500
                        if prize_index < 5:
                            async with lock:
                                bonus_back += values[prize_index] * 0.8
                        elif prize_index == 6:
                            async with lock:
                                upload_in_gb += 10
                        elif prize_index == 5:
                            async with lock:
                                upload_in_gb += 20
                else:
                    return None
            else:
                logger.warn(f"Request failed with status {response.status}")
                return None


# 主函数
async def main(number):
    global cost
    global bonus_back
    global upload_in_gb
    global prize_counts

    a = time.time()
    await tspin_the_wheel(number)
    adjusted_cost = cost - bonus_back
    logger.info(f"Total cost: {cost}")
    logger.info(f"Adjusted cost: {adjusted_cost}")
    logger.info(f"Bonus back: {bonus_back}")
    logger.info(f"Upload in GB: {upload_in_gb}")
    logger.info(time.time() - a)
    for prize, count in prize_counts.items():
        logger.info(f"Prize {prizes[prize]}: {count}")

    prize_counts_re = prize_counts
    upload_in_gb_re = upload_in_gb
    cost_re = cost
    bonus_back_re = bonus_back

    bonus_back = 0
    cost = 0
    upload_in_gb = 0
    prize_counts = {k: 0 for k in prizes.keys()}

    return cost_re, bonus_back_re, upload_in_gb_re, prize_counts_re


# 运行主函数
if __name__ == "__main__":
    asyncio.run(main(total_spins))
